package com.group02.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.entity.user.SysUser;
import com.group02.community.entity.vo.queryVO.QueryParamsVO;
import com.group02.community.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api
@CrossOrigin
@RequestMapping("user")
@RestController
public class SUserController {

    @Autowired
    IUserService userService;

    @PostMapping("users")
    public ResponseEntity<IPage<SysUser>> getUserPage(Long current, Long size , String date, QueryParamsVO qp){
        if (date!=null && date!=""){
            String[] split = date.split(",");
            qp.setStartTime(split[0]);
            qp.setEndTime(split[1]);
        }
        IPage<SysUser> page = userService.getUserPage(current,size,qp);
        return ResponseEntity.ok(page);
    }

    @PostMapping("user")
    public ResponseEntity<String> addUser(SysUser user){
        String s = userService.saveAllUserMsg(user);
        return ResponseEntity.ok(s);
    }

    @GetMapping("RAP")
    public ResponseEntity<Map<String,Long>> getUserRoleAndPost(Long id){
        Map<String,Long> map = userService.getUserRoleAndPost(id);
        return ResponseEntity.ok(map);
    }
    @PutMapping("user")
    public ResponseEntity<String> updateUserStatus(SysUser user){
        String msg=userService.updateAllUserMsg(user);
        return ResponseEntity.ok("OK");
    }

    @DeleteMapping("user/{id}")
    public ResponseEntity<String> deleteById(Long id){
        userService.removeById(id);
        return ResponseEntity.ok("OK");
    }

    @DeleteMapping("deleteAll/{ids}")
    public ResponseEntity<String> deleteByIds(@PathVariable String ids){
//        String[] split = ids.split(",");
//        List<String> strings = Arrays.asList(split);
        ids="("+ids+")";
        userService.removeByAllIds(ids);
        return ResponseEntity.ok("OK");
    }


    @GetMapping("excel")
    public ResponseEntity<String> exportExcel(HttpServletResponse resp) throws IOException {
        userService.exportExcel(resp);
        return ResponseEntity.ok("");
    }


    @ApiOperation("导入")
    @PostMapping("import")
    public ResponseEntity<String> importExcel(MultipartFile file){
        userService.importExcel(file);
        return ResponseEntity.ok("导入成功");
    }

}

